package com.lh_travel.lohas.activity.user;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageButton;

import com.lh_travel.lohas.R;
import com.lh_travel.lohas.adapter.MyExpandableAdapter;
import com.lh_travel.lohas.base.api;
import com.lh_travel.lohas.base.baseActivity;
import com.lh_travel.lohas.domain.Comment;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxListCallback;

import java.util.List;

public class QuestionListActivity extends baseActivity {

    private Toolbar toolbar;
    private ImageButton img_back;
    private ExpandableListView elv_quest;
    private List<Comment> questList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_questlist);

        findView();
        bindListner();
        ensureUI();
    }

    @Override
    public void findView() {
        elv_quest = (ExpandableListView) findViewById(R.id.elv_quest);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        img_back = (ImageButton) findViewById(R.id.img_back);

    }

    @Override
    public void bindListner() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void ensureUI() {
        setSupportActionBar(toolbar);
        toolbar.setEnabled(true);

        new api(mContext).getQuestion(questCallBack);
    }

    private void initXrv() {

        MyExpandableAdapter adapter = new MyExpandableAdapter(mContext, questList);

        elv_quest.setAdapter(adapter);

        elv_quest.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                elv_quest.collapseGroup(groupPosition);

                return false;
            }
        });

    }

    private RxListCallback<List<Comment>> questCallBack = new RxListCallback<List<Comment>>() {
        @Override
        public void onNext(Object tag, int code, String message, List<Comment> response) {

            if (code == 200){

                questList = response;
                initXrv();
            }else {

                showMessage(message);

            }

        }

        @Override
        public void onError(Object tag, Throwable e) {

        }

        @Override
        public void onCancel(Object tag, Throwable e) {

        }
    };


}
